/*
 * 文件名： CiticExpayHelper.java
 * 
 * 工程名称: bis-bank-citic
 *
 * Gopay
 *
 * 创建日期： 2016年6月21日
 *
 * Copyright(C) 2016, by www.gopay.com.cn Inc.All rights reserved.
 *
 * 原始作者: 宣广海
 *
 */
package com.gopay.bis.citic.expay.operation;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.citic.expay.CiticExpay;
import com.gopay.common.exception.GopayException;

/**
 * 中信银行快捷支付操作类
 *
 * @author 宣广海
 *
 * @version
 *
 * @since 2016年6月21日
 */
public class CiticExpayCertHelper  {
	
	public byte[] sign(BaseMessage bsaeMessage) throws GopayException {

		CiticExpay citicExpay = (CiticExpay) bsaeMessage;
		byte[] b_out = null;

		try {

			String plainMessage = citicExpay.getPlainMessage();
			/**
			 * 打印支付请求明文报文
			 */
			BankLogUtils.printExPayPlainLog(citicExpay);
			/**
			 * 支付请求报文签名及加密
			 */
			b_out = CiticExpaySignUtil.sign_crypt(plainMessage, citicExpay.getPrivatePwd(),
					citicExpay.getPrivateKeyPath(),citicExpay.getPrivateKeyPath2nd() ,citicExpay.getPublicKeyPath() );

//			String signMessage = new String(b_out);
//
//			BankLogUtils.printExPaySignatureLog(citicExpay, signMessage);

			return b_out;

		} catch (Exception e) {
			BankLogUtils.printExPayExceptionLog(citicExpay, e);

			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6057);
		}
	}

	public Object verify(BaseMessage bsaeMessage,byte[] returnMessage) throws GopayException {

		CiticExpay citicExpay = (CiticExpay) bsaeMessage;

		byte[] b_out = null;

		String plainMessage = null;
		try {
			/**
			 * 响应报文解密及验签
			 */
			b_out = CiticVerifyUtil.decrypt_verify(returnMessage, citicExpay.getPrivatePwd(),
					citicExpay.getPrivateKeyPath(), citicExpay.getPublicKeyPath());

			if (null != b_out) {
				plainMessage = new String(b_out);
			} else {
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E6079);
			}

			BankLogUtils.printExPayVerifySignatureLog(citicExpay, plainMessage);

			return plainMessage;

		} catch (Exception e) {
			BankLogUtils.printExPaySignExceptionLog(citicExpay, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6079);
		}
	}
}
